package com.zyk.leetcode;

import java.util.Stack;

/**
 * @author zhangsan
 * @date 2021/4/3 21:08
 */
public class J09 {

    /** https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/ */
    static class CQueue {

        Stack<Integer> stack1;
        Stack<Integer> stack2;

        public CQueue() {
            stack1 = new Stack<>();
            stack2 = new Stack<>();
        }

        public void appendTail(int value) {
            stack1.push(value);
        }

        public int deleteHead2() {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
            int v = stack2.isEmpty()? -1: stack2.pop();
            while (!stack2.isEmpty()) {
                stack1.push(stack2.pop());
            }

            return v;
        }

        // 不用每次都维护, 当他为空了,在一次给他赋值.减少了操作
        public int deleteHead() {
            if(!stack2.isEmpty()){
                return stack2.pop();
            }else{
                while(!stack1.isEmpty()){
                    stack2.push(stack1.pop());
                }
                return stack2.isEmpty() ? -1 : stack2.pop();
            }
        }
    }

    // for test
    public static void main(String[] args) {
        // 输入：
        //["CQueue","appendTail","deleteHead","deleteHead"]
        //[[],[3],[],[]]
        //输出：[null,null,3,-1]

        CQueue queue = new CQueue();
        queue.appendTail(1);
        queue.appendTail(2);
        queue.appendTail(3);
        System.out.println(queue.deleteHead());
        queue.appendTail(4);
        queue.appendTail(5);
        System.out.println(queue.deleteHead());
        queue.appendTail(6);
        queue.appendTail(7);
        System.out.println(queue.deleteHead());
        System.out.println(queue.deleteHead());
        System.out.println(queue.deleteHead());
        System.out.println(queue.deleteHead());
        System.out.println(queue.deleteHead());
        System.out.println(queue.deleteHead());
        System.out.println(queue.deleteHead());
        System.out.println(queue.deleteHead());
        System.out.println(queue.deleteHead());

    }

}
